// pages/my/my.js

const app = getApp()
Page({
  data: {
      user : [],
      upwd:'',
      userimg: ''
  },

headimage: function () {  
    var that = this;
		wx.showActionSheet({
			itemList: ['从相册中选择', '拍照'],
			// itemColor: "#f7982a",
			success: function(res) {
				if (!res.cancel) {
					if (res.tapIndex == 0) {
						that.chooseWxImageShop('album'); //从相册中选择
					} else if (res.tapIndex == 1) {
						that.chooseWxImageShop('camera'); //手机拍照
					}
				}
			}
    })
  },
  chooseWxImageShop: function(type) {
    var that = this;
		wx.chooseImage({
			sizeType: ['original', 'compressed'],
			sourceType: [type],
			success: function(res) {
        const filePath = res.tempFilePaths[0];
				wx.uploadFile({
          url: 'http://47.94.153.89:9000/farm.php/api/upload',
          filePath: filePath,
          name:'avatar',
          header:{
            'content-type': 'multipart/form-data',
            "X-Token-Id" : wx.getStorageSync('token'),
            "X-Requested-With": "XMLHttpRequest"
          },
          success:function(res) {
            var res = JSON.parse(res.data);
           
            that.setData({
              "user.avatar" : res.data.avatar
            })
            wx.setStorageSync('user', that.data.user)
          }
      })
			}
		})
  },

  onLoad(options) {
    this.setData({
      user: wx.getStorageSync('user')
    })
  },

  kefu(){
    wx.showModal({
      title: '客服电话',
      content: '12345678989',
      success: function (res) {
        if (res.confirm) {//这里是点击了确定以后
            
        } else {//这里是点击了取消以后
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})